package com.hnevc.androidui10;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class WeatherActivity extends AppCompatActivity {
    Button mBtnSh, mBtnBj, mBtnGz;
    TextView mTvWeather,mTvName,mTvWind,mTvPm,mTvTemp;
    ImageView iv_icon;
    List<Weather> weatherList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather);

        initData();//初始化数据
        initView();
        initEvent();

    }

    private void initEvent() {
        mBtnSh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Weather w = weatherList.get(0);
                mTvWeather.setText(w.getWeather());
                mTvName.setText(w.getName());
                mTvPm.setText(w.getPm());
                mTvTemp.setText(w.getTemp());
                mTvWind.setText(w.getWind());
                iv_icon.setImageResource(R.drawable.cloud_sun);
            }
        });
        mBtnBj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Weather w = weatherList.get(1);
                mTvWeather.setText(w.getWeather());
                mTvName.setText(w.getName());
                mTvPm.setText(w.getPm());
                mTvTemp.setText(w.getTemp());
                mTvWind.setText(w.getWind());
                iv_icon.setImageResource(R.drawable.sun);
            }
        });
        mBtnGz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Weather w = weatherList.get(2);
                mTvWeather.setText(w.getWeather());
                mTvName.setText(w.getName());
                mTvPm.setText(w.getPm());
                mTvTemp.setText(w.getTemp());
                mTvWind.setText(w.getWind());
                iv_icon.setImageResource(R.drawable.clouds);
            }
        });

    }

    private void initView() {
        mBtnBj = findViewById(R.id.btn_bj);
        mBtnGz = findViewById(R.id.btn_gz);
        mBtnSh = findViewById(R.id.btn_sh);
        mTvWeather = findViewById(R.id.tv_weather);
        mTvWind = findViewById(R.id.tv_wind);
        mTvPm = findViewById(R.id.tv_pm);
        mTvTemp = findViewById(R.id.tv_temp);
        mTvName = findViewById(R.id.tv_city);
        iv_icon=findViewById(R.id.iv_icon);

    }

    private void initData() {
        weatherList = new ArrayList<>();
//解析weather2.json文件,将解析到Warther类型的对象
        //加入链表
        try {
            InputStream in = this.getResources().openRawResource(R.raw.weather2);

            //定义一个长度为:文件字节长度的字节数组bytes
            byte[] bytes = new byte[in.available()];
            in.read(bytes);//从输入流中读取字节,
            //将字节数组转换为字符串
            String temp = new String(bytes);
            JSONArray jsonArray = new JSONArray(temp);
            for (int i = 0; i < jsonArray.length(); i++) {
                //从就jsonObjectd对象中获取key的value
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String temp1 = jsonObject.getString("temp");
                String weather = jsonObject.getString("weather");
                String name = jsonObject.getString("name");
                String pm = jsonObject.getString("pm");
                String wind = jsonObject.getString("wind");


                Weather w = new Weather();
                w.setName(name);
                w.setPm(pm);
                w.setTemp(temp1);
                w.setWeather(weather);
                w.setWind(wind);

                weatherList.add(w);
            }

        } catch (Exception e) {

        }
    }
}
